<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Amitav Roy
 * Date: 1/27/13
 * Time: 9:41 AM
 * To change this template use File | Settings | File Templates.
 */
class Taxonomy_model extends CI_Model {
  // loading the parent construct
  public function __construct() {
    parent::__construct();
  }

  // load the term data based on tid
  public function get_term($nid) {
    return $this->attach_term_to_node($nid);
  }

  // build the term for the node object
  protected function attach_term_to_node($nid) {
    $fields = array();

    $this->db->where('nid', $nid);

    $this->db->join('taxonomy_term_data as td', 'td.tid = ti.tid');

    array_push($fields, 'td.tid', 'td.vid', 'td.name');

    $this->db->where('td.vid', 1);

    $this->db->select($fields);

    $query = $this->db->get('taxonomy_index as ti');
    $result = $query->result_array();

    return $result;
  }

  public function term_article_list($tid, $vocab = 'tags') {
    $this->db->select('n.nid');
    $this->db->from('taxonomy_index ti');

    $this->db->join('node n', 'ti.nid = n.nid');
    $this->db->join('taxonomy_term_data td', 'td.tid = ti.tid','inner');

    $this->db->where('td.tid', $tid);
    //$this->db->where('td.name', $term);

    $this->db->order_by('n.created', 'desc');
    $this->db->limit(10);

    $query = $this->db->get();
    $query = $query->result_array();
    //dd($query);
    return $query;
  }
}
